package com.liuyanzhao.ssm.blog.controller.home;

import com.liuyanzhao.ssm.blog.entity.UserArticle;
import com.liuyanzhao.ssm.blog.service.ArticleService;
import com.liuyanzhao.ssm.blog.service.UserArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
public class UserArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private UserArticleService userArticleService;

    @RequestMapping(value = "/addFavorite", method = RequestMethod.POST)
    @ResponseBody
    public String addFavorite(@RequestBody Map<String, Integer> params) {
        // 处理JSON参数
        System.out.println("hhhh");
        int userId = params.get("userId");
        int articleId = params.get("articleId");
        System.out.println(userId + " " + articleId);
        System.out.println("pre");
        userArticleService.insert(new UserArticle(userId,articleId));
        System.out.println("done");
        return "success";
    }
    @RequestMapping(value = "/removeFavorite", method = RequestMethod.POST)
    @ResponseBody
    public String removeFavorite(@RequestBody Map<String, Integer> params) {
        // 处理JSON参数
        System.out.println("cnm");
        int userId = params.get("userId");
        int articleId = params.get("articleId");
        System.out.println(userId + " " + articleId);
        userArticleService.delete(new UserArticle(userId,articleId));
        return "success";
    }
}
